package com.wqtz.chechai.view.activities;

import android.content.Intent;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.avos.avoscloud.AVException;
import com.avos.avoscloud.AVFile;
import com.avos.avoscloud.AVObject;
import com.avos.avoscloud.AVUser;
import com.avos.avoscloud.SaveCallback;
import com.bigkoo.svprogresshud.SVProgressHUD;
import com.orhanobut.logger.Logger;
import com.qiniu.android.http.ResponseInfo;
import com.wqtz.chechai.R;
import com.wqtz.chechai.base.BaseActivity;
import com.wqtz.chechai.bean.CarResult;
import com.wqtz.chechai.presenter.BasePresent;
import com.wqtz.chechai.utils.QiNiuUtils;
import com.wqtz.chechai.utils.ToastUtils;

import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * 项目名称： CheChai
 * 创建人 : Liberation
 * 创建时间: 2017/1/3 14:43
 */

public class DrivingLicenseActivity extends BaseActivity {
	@BindView(R.id.iv_back)
	ImageView mIvBack;
	@BindView(R.id.tv_title)
	TextView mTvTitle;
	@BindView(R.id.iv_vin)
	ImageView mIvVin;
	@BindView(R.id.iv_pinggu)
	ImageView mIvPinggu;
	@BindView(R.id.iv_driving_info)
	ImageView mIvDrivingInfo;
	@BindView(R.id.tv_vin)
	TextView mTvVin;
	@BindView(R.id.tv_edpg)
	TextView mTvEdpg;
	@BindView(R.id.tv_carinfo)
	TextView mTvCarinfo;
	@BindView(R.id.tv_es)
	TextView mTvEs;
	@BindView(R.id.carbook1)
	TextView mCarbook1;
	@BindView(R.id.iv_cha_pic)
	ImageView mIvChaPic;
	@BindView(R.id.tv_cha_next)
	TextView mTvChaNext;
	@BindView(R.id.ll_driving_upload_1)
	LinearLayout mLlDrivingUpload1;
	@BindView(R.id.catboot2)
	TextView mCatboot2;
	@BindView(R.id.iv_cha_pic_2)
	ImageView mIvChaPic2;
	@BindView(R.id.tv_cha_next_2)
	TextView mTvChaNext2;
	@BindView(R.id.ll_driving_upload_2)
	LinearLayout mLlDrivingUpload2;
	@BindView(R.id.tv_chadang_status)
	TextView mTvChadangStatus;
	@BindView(R.id.tv_cha_next_status)
	TextView mTvChaNextStatus;
	@BindView(R.id.ll_driving_status)
	LinearLayout mLlDrivingStatus;
	@BindView(R.id.btn_confirm)
	Button mBtnConfirm;
	@BindView(R.id.tv_vin_error_return)
	TextView mTvVinErrorReturn;
	private ArrayList<String> defaultDataArray;
	private final int REQUEST_STATUS = 1012;//获取抵押状态
	private ArrayList<String> path; //选中获拍摄的正本图片集合
	private String cityId;//购买地点城市ID
	private String carModelId; //车辆型号对应的ID
	private String carModel;//车辆型号
	private String year; //购买年份
	private String month;//购买月份
	private String mileage;//行驶的公里数
	private String price;//购买的价钱
	private String carStatus; //车辆状态
	private String provinceId; //省份ID
	private Double assumPrice; //预估价格
	private CarResult mResult;
	private String carBuyTime;//购买时间
	private String price1, price2, price3;//三家预估价格
	private List<String> upLoadfileNames; //上传到服务器的驾照名字

	private final int TAKEPHOTO1 = 1021;//正本请求码
	private final int TAKEPHOTO2 = 1022; //副本请求码
	private final int GETCARMORTAGESTATUS = 1023; //获取车辆抵押状态
	private ArrayList<String> images1; //驾照正本照片本地地址集合
	private ArrayList<String> images2; //驾照副本照片本地地址集合
	private ArrayList<String> qiImages1 = new ArrayList<>(); //驾照正本照片七牛地址集合
	private ArrayList<String> qiImages2 = new ArrayList<>(); //驾照副本照片七牛地址集合
	private String carMortgageStatus;//车辆抵押状态
	private Boolean book1IsOk = false;
	private Boolean book2IsOk = false;
	private SVProgressHUD svProgressHUD;


	@Override
	public void init() {
		ButterKnife.bind(this);
		Intent intent = getIntent();
		cityId = intent.getStringExtra("cityId");
		provinceId = intent.getStringExtra("provinceId");
		carModelId = intent.getStringExtra("carModelId");
		carModel = intent.getStringExtra("carModel");
		carBuyTime = intent.getStringExtra("buyTime");
		mileage = intent.getStringExtra("mileage");
		carStatus = intent.getStringExtra("carStatus");
		price1 = intent.getStringExtra("price1");
		price2 = intent.getStringExtra("price2");
		price3 = intent.getStringExtra("price3");
		assumPrice = intent.getDoubleExtra("assumPrice", 0);
		images1 = new ArrayList<>();
		images2 = new ArrayList<>();
	}

	@Override
	public void loadData() {

	}

	@Override
	public int setContentViews() {
		return R.layout.acttivity_driving;
	}

	@Override
	public BasePresent createPresenter() {
		return null;
	}


	@OnClick({R.id.iv_back, R.id.ll_driving_upload_1, R.id.ll_driving_upload_2, R.id.ll_driving_status, R.id.btn_confirm, R.id.tv_vin_error_return})
	public void onClick(View view) {
		switch (view.getId()) {
			case R.id.iv_back:
				finish();
				break;
			case R.id.ll_driving_upload_1:
				Intent intent = new Intent(this, DriverBook1Activity.class);
				intent.putStringArrayListExtra("images1", images1);
				startActivityForResult(intent, TAKEPHOTO1);
				//getPhoto1();
				break;
			case R.id.ll_driving_upload_2:
				Intent intent2 = new Intent(this, DriverBook2Activity.class);
				intent2.putExtra("images2", images2);
				startActivityForResult(intent2, TAKEPHOTO2);
				break;
			case R.id.ll_driving_status:
				Intent intent3 = new Intent(this, CarMortgageStatusActivity.class);
				startActivityForResult(intent3, GETCARMORTAGESTATUS);
				break;
			case R.id.btn_confirm:
				if (images1.size() == 0 || images2.size() == 0) {
					ToastUtils.show(this, "资料不完整", 2000);
				} else {
					uploadData();
				}
				break;
			case R.id.tv_vin_error_return:
				finish();
				break;
		}
	}

	private void upDataToLeanCloud() {
		/*private String cityId;//购买地点城市ID
		private String carModelId; //车辆型号对应的ID
		private String carModel;//车辆型号
		private String year; //购买年份
		private String month;//购买月份
		private String mileage;//行驶的公里数
		private String price;//购买的价钱
		private String carStatus; //车辆状态
		private String provinceId; //省份ID
		private Double assumPrice; //预估价格
		private CarResult mResult;
		private String carBuyTime;//购买时间
		private String price1, price2, price3;//三家预估价格
		private List<String> upLoadfileNames; //上传到服务器的驾照名字

		private final int TAKEPHOTO1 = 1021;//正本请求码
		private final int TAKEPHOTO2 = 1022; //副本请求码
		private final int GETCARMORTAGESTATUS = 1023; //获取车辆抵押状态
		private ArrayList<String> images1; //驾照正本照片地址集合
		private ArrayList<String> images2; //驾照副本照片地址集合
		private String carMortgageStatus;//车辆抵押状态*/
		AVObject product = new AVObject("Order");
		product.put("cityId",cityId);
		product.put("carModelId",carModelId);
		product.put("carModel", carModel);
		product.put("owner", AVUser.getCurrentUser());
		product.put("carBuyTime", carBuyTime);
		product.put("buyPrice", price);
		product.put("price1", price1);
		product.put("price2", price2);
		product.put("price3", price3);
		product.put("carStatus", carStatus);
		product.put("provinceId", provinceId);
		product.put("assumPrice", assumPrice);
		product.put("images1","7y8l76.com1.z1.glb.clouddn.com/"+qiImages1.get(0));
		product.put("images2", "7y8l76.com1.z1.glb.clouddn.com/"+qiImages2.get(0));
		product.saveInBackground(new SaveCallback() {
			@Override
			public void done(AVException e) {
				if (e == null) {
					svProgressHUD = new SVProgressHUD(DrivingLicenseActivity.this);
					svProgressHUD.showSuccessWithStatus("提交完成");
				} else {
					svProgressHUD.showSuccessWithStatus("提交失败");
				}
			}
		});

	}

	private void uploadData() {
		qiImages1.clear();
		qiImages2.clear();
		qiImages1.addAll(makeUploadFileName("carBook1", images1));
		qiImages2.addAll(makeUploadFileName("carBook2", images2));
		QiNiuUtils qiniu1 = new QiNiuUtils(images1, qiImages1, new QiNiuUtils.isUploadSuccess() {
			@Override
			public void isUploadSusscess(String key, ResponseInfo info, JSONObject response) {
				if (info.isOK()) {
					Logger.e("驾照正本上传完毕" + info.toString());
					book1IsOk = true;
					if(book1IsOk&&book2IsOk&&(null!=carMortgageStatus)){
						String path = info.toString();
						Logger.e("返回的下载地址:"+path);
						upDataToLeanCloud();
					}
				}
			}

			@Override
			public void upLoadProgress(String key, double progress) {
				Logger.e(progress*100+key+"正本");
			}
		});
		QiNiuUtils qiniu2 = new QiNiuUtils(images2,qiImages2, new QiNiuUtils.isUploadSuccess() {
			@Override
			public void isUploadSusscess(String key, ResponseInfo info, JSONObject response) {
				if (info.isOK()) {
					Logger.e("驾照副本上传完毕" + info.toString());
					book2IsOk = true;
					if(book1IsOk&&book2IsOk&&(null!=carMortgageStatus)){
						upDataToLeanCloud();
					}
				}
			}


			@Override
			public void upLoadProgress(String key, double progress) {
				Logger.e(progress*100+key+"副本");

			}
		});


	}


	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		super.onActivityResult(requestCode, resultCode, data);
		if (resultCode == RESULT_OK) {
			switch (requestCode) {
				case TAKEPHOTO1: {
					mIvChaPic.setVisibility(View.VISIBLE);
					images1.addAll(data.getStringArrayListExtra("images1"));
					Logger.e(images1.toString());
				}
				break;
				case TAKEPHOTO2: {
					mIvChaPic2.setVisibility(View.VISIBLE);
					images2.addAll(data.getStringArrayListExtra("images2"));
				}
				break;
				case REQUEST_STATUS: {
				}
				break;
				case GETCARMORTAGESTATUS: {
					carMortgageStatus = data.getStringExtra("mortgageStatus");
					mTvChaNextStatus.setText(carMortgageStatus);
					Logger.e(carMortgageStatus);
				}
				break;
			}
		}

	}

	private ArrayList<String> makeUploadFileName(String carBook, List<String> localpath) {
		//文件名构造
		ArrayList<String> finelists = new ArrayList<>();
		if (localpath.size() == 0) {
			return null;
		}
		for (int i = 0; i < localpath.size(); i++) {
			StringBuffer stringBuffer = new StringBuffer();
			stringBuffer.append(carBook).
					append(localpath.get(i));//取本地图库文件名为七牛存储文件名
			       /*append(cityId).
					 append(".").append(provinceId).append(".")
					.append(carModelId).append(".")
					.append(mileage).append(".")
					.append(carStatus).append(".")
					.append(assumPrice).append(".")
					.append(price1).append(".")
					.append(price2).append(".")
					.append(price3).append(".")*/

			finelists.add(stringBuffer.toString());
		}

		return finelists;
	}

	@Override
	protected void onResume() {
		super.onResume();
		activitys.add(this); //添加当前的activity
		if (images1.size() == 0) {
			mIvChaPic2.setVisibility(View.INVISIBLE);
		} else {
			mIvChaPic2.setVisibility(View.VISIBLE);
		}
		if (images2.size() == 0) {
			mIvChaPic2.setVisibility(View.INVISIBLE);
		} else {
			mIvChaPic2.setVisibility(View.VISIBLE);
		}
	}

}
